package com.microframework.boot.collect.controller;

import java.sql.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson2.JSONObject;
import com.jfinal.kit.JsonKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.microframework.base.core.datasource.annotation.Tx;
import com.microframework.base.core.util.SecurityUtils;
import com.microframework.base.core.util.UuidUtil;
import com.microframework.base.web.page.PageQuery;
import com.microframework.base.web.resp.R;
import com.microframework.boot.collect.model.domain.DataSourceConfig;
import com.microframework.boot.collect.service.IDataSourceConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;


/**
 * 数据采集数据源配置管理
 */
@Slf4j
@RestController
@RequestMapping("/data-collect/config")
@Tag(name = "数据源配置管理", description = "数据采集数据源配置管理相关接口")
public class DataSourceConfigController {
	

	@Autowired
	private IDataSourceConfigService dataSourceConfigService;
	
    @PostMapping("/pages")
    @Operation(summary = "分页查询数据源配置列表", description = "分页查询采集数据源配置数据")
    public R<Page<DataSourceConfig>> pages(@RequestBody PageQuery<DataSourceConfig> pageQuery) {
        return R.ok(dataSourceConfigService.pages(pageQuery));
    }
    
	
	
    @Operation(summary = "保存数据源配置")
	@Parameters({ @Parameter(name = "dataSourceConfig", description = "采集数据源配置信息", required = true) })
    @PostMapping("/save")
    @Tx(value="micro_collect")
    public R<DataSourceConfig> save(HttpServletRequest request, @RequestBody DataSourceConfig dataSourceConfig) {
		try {
			
			System.out.println(JsonKit.toJson(dataSourceConfig));
			
			if (StrKit.notBlank(dataSourceConfig.getId())) {
				dataSourceConfig.setModifyTime(new Date(System.currentTimeMillis()));
				dataSourceConfig.setModifyUserId(SecurityUtils.getUserId());
				if (dataSourceConfig.update()) {
					return R.ok(dataSourceConfig);
				} else {
					return R.fail("更新失败.");
				}
			} else {
				dataSourceConfig.setId(UuidUtil.getUUID());
				dataSourceConfig.setCreateTime(new Date(System.currentTimeMillis()));
				dataSourceConfig.setCreateUserId(SecurityUtils.getUserId());
				if (dataSourceConfig.save()) {
					return R.ok(dataSourceConfig);
				} else {
					return R.fail("保存失败.");
				}
			}
		} catch (Exception e) {
			throw e;
		}
    }
    
	@Operation(summary = "删除数据源配置")
	@Parameters({ @Parameter(name = "id", description = "数据源配置id", required = true) })
	@DeleteMapping("/{id}")
	public R<String> deleteById(@PathVariable("id") String id) {
		try {
			if(DataSourceConfig.dao.deleteById(id)) {
				return R.ok("删除成功.");
			}else {
				return R.fail("删除失败.");
			}
		}catch (Exception e) {
			throw e;
		}
	}
	
	@Operation(summary = "查询数据源配置信息")
	@Parameters({ @Parameter(name = "id", description = "数据源配置id", required = true) })
	@GetMapping("/{id}/info")
	public R<DataSourceConfig> getDetailInfo(@PathVariable("id") String id) {
		try {
			DataSourceConfig dataSourceConfig=DataSourceConfig.dao.findById(id);
			if(dataSourceConfig!=null) {
				return R.ok(dataSourceConfig);
			}else {
				return R.fail("未查询到数据.");
			}
		}catch (Exception e) {
			throw e;
		}
	}
	
	@Operation(summary = "查询数据源配置信息")
	@Parameters({ @Parameter(name = "id", description = "数据源配置id", required = true),
		 @Parameter(name = "type", description = "数据源类型", required = true)	
	})
	@GetMapping("/{id}/{type}/test")
	public R<JSONObject> test(@PathVariable("id") String id,@PathVariable("type") String type) throws Exception {
		if(StrKit.equals("HTTP", type)) {
			return R.ok(dataSourceConfigService.check(id));
		}
		return R.fail("无效数据源类型.");
	}
}
